package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduStudentDTO;
import com.zenith.edu.po.vo.EduStudentVO;
import com.zenith.edu.service.EduStudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/student")
@Tag(name = "学生管理", description = "学生管理接口")
public class EduStudentController {
    
    @Autowired
    private EduStudentService eduStudentService;
    
    @SecurePostMapping(value = "/createStudent", roles = "ROLE_ADMIN", permission = "student:create")
    @Operation(summary = "创建学生")
    public Result<EduStudentVO> createStudent(@RequestBody EduStudentDTO dto) {
        EduStudentVO studentVO = eduStudentService.createStudent(dto);
        return Result.success(studentVO);
    }
    
    @SecurePostMapping(value = "/getStudentById/{id}", roles = "ROLE_ADMIN", permission = "student:read")
    @Operation(summary = "根据ID获取学生详情")
    public Result<EduStudentVO> getStudentById(@PathVariable Long id) {
        EduStudentVO studentVO = eduStudentService.getStudentById(id);
        if (studentVO == null) {
            return Result.fail("学生不存在");
        }
        return Result.success(studentVO);
    }
    
    @SecurePostMapping(value = "/updateStudent", roles = "ROLE_ADMIN", permission = "student:update")
    @Operation(summary = "更新学生信息")
    public Result<EduStudentVO> updateStudent(@RequestBody EduStudentDTO dto) {
        EduStudentVO studentVO = eduStudentService.updateStudent(dto);
        return Result.success(studentVO);
    }
    
    @SecurePostMapping(value = "/deleteStudent/{id}", roles = "ROLE_ADMIN", permission = "student:delete")
    @Operation(summary = "删除指定学生")
    public Result<Void> deleteStudent(@PathVariable Long id) {
        boolean result = eduStudentService.deleteStudent(id);
        if (!result) {
            return Result.fail("学生删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getStudentList", roles = "ROLE_ADMIN", permission = "student:read")
    @Operation(summary = "获取学生列表")
    public Result<List<EduStudentVO>> getStudentList() {
        List<EduStudentVO> studentList = eduStudentService.getStudentList();
        return Result.success(studentList);
    }
}